<?php

/**
 * This is the model class for table "{{news_cat}}".
 *
 * The followings are the available columns in table '{{news_cat}}':
 * @property integer $znc_id
 * @property integer $znc_type
 * @property string $znc_title
 * @property string $znc_childid
 * @property integer $znc_patentid
 * @property integer $znc_topid
 * @property integer $znc_listorder
 * @property integer $znc_status
 * @property integer $inputtime
 */
class NewsCat extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return NewsCat the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{news_cat}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('znc_patentid, znc_topid, znc_listorder, znc_status, inputtime', 'numerical', 'integerOnly'=>true),
			array('znc_title', 'length', 'max'=>25),
			array('znc_childid', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('znc_id, znc_type,znc_title, znc_childid, znc_patentid, znc_topid, znc_listorder, znc_status, inputtime', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'znc_id' => 'Znc',
			'znc_title' => 'Znc Title',
			'znc_childid' => 'Znc Childid',
			'znc_patentid' => 'Znc Patentid',
			'znc_topid' => 'Znc Topid',
			'znc_listorder' => 'Znc Listorder',
			'znc_status' => 'Znc Status',
			'inputtime' => 'Inputtime',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('znc_id',$this->znc_id);
		$criteria->compare('znc_type',$this->znc_type,true);
		$criteria->compare('znc_title',$this->znc_title,true);
		$criteria->compare('znc_childid',$this->znc_childid,true);
		$criteria->compare('znc_patentid',$this->znc_patentid);
		$criteria->compare('znc_topid',$this->znc_topid);
		$criteria->compare('znc_listorder',$this->znc_listorder);
		$criteria->compare('znc_status',$this->znc_status);
		$criteria->compare('inputtime',$this->inputtime);

		$criteria->order = 'znc_listorder ASC';
		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
			'pagination'=>array(
					'pageSize'=>100,
			)
		));
	}
	
	/**
	 * 保存前的操作
	 */
	protected function beforeSave(){
		if($this->isNewRecord){
			if($this->znc_patentid!=0){
				//取分类顶级ID
				$parent = $this->findByPk($this->znc_patentid);
				$this->znc_topid = $parent->znc_patentid;
			}
		}
		return true;
	}
	
	/**
	 * 保存后的操作
	 * 1.更新对应父分类的子分类
	 */
	protected function afterSave(){
		if($this->isNewRecord){
			if($this->znc_patentid>0){
				//分类父级
				$parent = $this->findByPk($this->znc_patentid);
				if($parent->znc_patentid>0){
					//分类顶级
					$top = $this->findByPk($parent->znc_patentid);
					if($top->znc_childid==null){
						$top->znc_childid = $this->znc_id;
					}else{
						$top->znc_childid = $top->znc_childid.','.$this->znc_id;
					}
					$top->save();
				}
				if($parent->znc_childid==null){
					$parent->znc_childid = $this->znc_id;
				}else{
					$parent->znc_childid = $parent->znc_childid.','.$this->znc_id;
				}
				$parent->save();
			}
		}
		$this->DelCatcache();
		return true;
	}
	
	/**
	 * 删除前的操作
	 * 更新相关父分类的子分类
	 */
	protected function beforeDelete(){
		if($this->znc_patentid>0){
			//分类父级
			$parent = $this->findByPk($this->znc_patentid);
			if($parent->znc_patentid>0){
				//分类顶级
				$top = $this->findByPk($parent->znc_patentid);
				$top->znc_childid = str_replace(','.$this->znc_id,'',$top->znc_childid);
				$top->znc_childid = str_replace($this->znc_id.',','',$top->znc_childid);
				$top->znc_childid = str_replace($this->znc_id,'',$top->znc_childid);
				$top->save();
			}
			$parent->znc_childid = str_replace(','.$this->znc_id,'',$parent->znc_childid);
			$parent->znc_childid = str_replace($this->znc_id.',','',$parent->znc_childid);
			$parent->znc_childid = str_replace($this->znc_id,'',$parent->znc_childid);
			$parent->save();
		}
		return true;
	}
	
	//清空缓存
	public function DelCatcache(){
		Yii::app()->filecache->delete('newscat');
	}

	protected function afterDelete(){
		//清空缓存
		$this->DelCatcache();
		return true;
	}	
	
	//资讯分类缓存，返回全部数据
	public function getNewsCat(){
		//如果存在缓存，调用缓存数据
		if($json = Yii::app()->filecache->get('newscat-cache')){
			return json_decode($json,true);
		}else{
			$criteria = new CDbCriteria;
			$criteria->compare('znc_patentid','0');
			$data = NewsCat::model()->findAll($criteria);
			foreach($data as $key=>$val){
				$item[$val['znc_id']] = $val->attributes;
				$criteria = new CDbCriteria;
				$criteria->compare('znc_patentid',$val->znc_id);
				$data2 = NewsCat::model()->findAll($criteria);
				foreach($data2 as $key2=>$val2){
					$item[$val['znc_id']]['subcat'][$val2['znc_id']] = $val2->attributes;
					$criteria = new CDbCriteria;
					$criteria->compare('znc_patentid',$val2->znc_id);
					$data3 = NewsCat::model()->findAll($criteria);
					if(!empty($data3)){
						foreach($data3 as $key3=>$val3){
							$item[$val['znc_id']]['subcat'][$val2['znc_id']]['subcat'][$val3['znc_id']] = $val3->attributes;
						}
					}
				}
			}
			$json = json_encode($item);
			//设置缓存
			Yii::app()->filecache->set('newscat-cache',$json,3600*24);
			return $item;
		}
	}
	//资讯分类缓存
	public function getNewsCategory(){
		//如果存在缓存，调用缓存数据
		if($json = Yii::app()->filecache->get('newscat')){
			return json_decode($json,true);
		}else{
			$criteria = new CDbCriteria;
			$criteria->compare('znc_patentid','0');
			$data = NewsCat::model()->findAll($criteria);
			$item['0'] = $data = CHtml::listData($data,'znc_id','znc_title');
			foreach($data as $key=>$val){
				$criteria = new CDbCriteria;
				$criteria->compare('znc_patentid',$key);
				$data2 = NewsCat::model()->findAll($criteria);
				if(!empty($data2))
					$item['0,'.$key] = $data2 = CHtml::listData($data2,'znc_id','znc_title');
				foreach($data2 as $key2=>$val2){
					$criteria = new CDbCriteria;
					$criteria->compare('znc_patentid',$key2);
					$data3 = NewsCat::model()->findAll($criteria);
					if(!empty($data3))
						$item['0,'.$key.",".$key2] = $data3 = CHtml::listData($data3,'znc_id','znc_title');
				}
			}
			$json = json_encode($item);
			//设置缓存
			Yii::app()->filecache->set('newscat',$json,3600*24);
			return $item;
		}
	}
}